//基于AES-256-CBC 模式加密
#include <iostream>
#include <string>
#include <cryptlib.h>
#include <osrng.h>
#include <aes.h>
#include <modes.h>
#include <filters.h>
#include <base64.h>
using namespace CryptoPP;
using namespace std;
string nax(const string& plaintext, const SecByteBlock& key, SecByteBlock& iv) {
    try 
    {
        AutoSeededRandomPool rng;
        rng.GenerateBlock(iv, AES::BLOCKSIZE);
        CBC_Mode<AES>::Encryption encryptor;
        encryptor.SetKeyWithIV(key, key.size(), iv);
        string ciphertext;
        StringSource(plaintext, true,
            new StreamTransformationFilter(encryptor, new StringSink(ciphertext)));
        string encoded;
        StringSource(iv + ciphertext, true, new Base64Encoder(new StringSink(encoded), false));
        return encoded;
    } 
    catch (const Exception& e) {
        cerr << "Encryption failed: " << e.what() << endl;
        exit(1);
    }
}
int main() 
{
    AutoSeededRandomPool rng;
    SecByteBlock key(AES::DEFAULT_KEYLENGTH);
    rng.GenerateBlock(key, key.size());
    string plaintext = "这是一个加密的安全消息！";
    SecByteBlock iv(AES::BLOCKSIZE);
    string encryptedMessage = nax(plaintext, key, iv);
    cout << "Original Message: " << plaintext << endl;
    cout << "Encrypted Message (Base64): " << encryptedMessage << endl;
    return 0;
}
